<?php
Class TaskSheetController extends Controller{

	public function show($id){
		$tasksheet = $this->loader->model('tasksheet');
		$tasksheet->SelectTaskSheetFromSql($id);
		$this->smarty->assign('title', $tasksheet->GetName());
			
		if(!empty($_POST)){
			$tasksheet->SubmitAnswer($_POST,$_SESSION['user_id']);
			Routing::redirect('?q=tasksheet/showall');
		}
		
		$task_attachment =  $tasksheet-> SelectTaskSheetAttachmentFromSql($id);
		$user_attachment = $tasksheet -> GetUserAttachment($id, '1');
		$this->smarty->assign('tasksheet', $tasksheet);
		$this->smarty->assign('task_attachment', $task_attachment);
		$this->smarty->assign('user_attachment', $user_attachment);
		$this->smarty->assign('title', 'Kémia éve - Feladatlap');
		
		$task = $this->loader->model('task');
		$tasks = $task->SelectAllTaskFromSql($id);
		$taskcount = count($tasks);
		$this->smarty->assign('tasks', $tasks);
		$this->smarty->assign('taskcount', $taskcount);
		$this->smarty->assign('is_upload', $tasks[0]['is_upload']);
		
		$answer = $this->loader->model('answer');
		$answers = $answer->SelectAllAnswerFromSql();
		
		for ($i=0;$i<count($tasks);$i++){
			$answers_temp[$i] = $answer->SelectAnswerFromSql($tasks[$i]['id']);
		}
		$already = $tasksheet->AlreadySubmited($answers_temp,$_SESSION['user_id']);
		if ($already==1 && $_SESSION['is_teacher']==0) Routing::redirect('?q=tasksheet/alreadysubmited'); 
		
		$answercount = count($answers);
		$this->smarty->assign('answers', $answers);
		$this->smarty->assign('answercount', $answercount);
		$this->smarty->assign('id', $id);
		
		$whocompleted = $tasksheet->WhoCompleted($id);
		$this->smarty->assign('whocompleted', $whocompleted);
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function showall(){
		$tasksheet = $this->loader->model('tasksheet');
		$tasksheets = $tasksheet->SelectAllTaskSheetFromSql();
		$tasksheetcount = count($tasksheets);
		
		$task = $this->loader->model('task');
		$answer = $this->loader->model('answer');
		for ($k=0;$k<count($tasksheets);$k++){
			$tasks = $task->SelectAllTaskFromSql($tasksheets[$k]['id']);
			for ($i=0;$i<count($tasks);$i++){
				$answers_temp[$i] = $answer->SelectAnswerFromSql($tasks[$i]['id']);
			}
			$already[$k] = $tasksheet->AlreadySubmited($answers_temp,$_SESSION['user_id']);
			$tasks = '';
			$answers_temp= '';
		}
		
		$this->smarty->assign('alreadysubmited', $already);
		$this->smarty->assign('tasksheets', $tasksheets);
		$this->smarty->assign('tasksheetcount', $tasksheetcount);
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
		$this->smarty->assign('title', 'Kémia éve - Feladatlapok');
	}
	
	public function finalize($id){
		$tasksheet = $this->loader->model('tasksheet');
		$tasksheet = $tasksheet->FinalizeTaskSheetInSql($id);
		Routing::redirect('?q=tasksheet/showall');
	}
	
	public function newtasksheetupload($tasksheet_id, $task_id=1){
		$sheet = $this->loader->model('tasksheet');
		$user_attachment = $sheet -> GetUserAttachment($tasksheet_id, '1');
		if(isset($_POST['submit'])){ //ha megnyomtuk a feltöltés gombot
			if(!file_exists('uploads/tasks/'.$tasksheet_id.'/'.$task_id.'/'.$_SESSION['user_id'])){
				mkdir('uploads/tasks/'.$tasksheet_id.'/'.$task_id.'/'.$_SESSION['user_id'], 0777);
			}
			else{
				unlink($user_attachment['url']);
				$sheet->DeleteUserAttachment($tasksheet_id, '1');
			}
			$target= 'uploads/tasks/'.$tasksheet_id.'/'.$task_id.'/'.$_SESSION['user_id'].'/'; //célmappa
			$file_name = Tools::clear_url(($_FILES['file']['name'])); //a célfájlt nevezze el a $_FILES superglobal változóban lévő fájlnévre (a fájl eredeti nevére)
			$tmp_dir = $_FILES['file']['tmp_name']; //az ideiglenes mappa helyét a $tmp_dir változóban tároljuk 
			if(!preg_match('/(doc|docx|rtf|pdf|odt|txt)$/i', $file_name))
			{
				$error = "Rossz fájltípus!"; //... "dobjon el" egy hibát
			}
				else
			{
				move_uploaded_file($tmp_dir, $target .$file_name); //az ideiglenes mappából átteszi a fájlt a végleges mappába (a $target . $file_name összeilleszti a két stringet, így uploads/fajlnev-et kapunk)
				$feltoltve = true; //a feltoltve változó true értéket kap
				$sheet-> InsertUserAttachment($tasksheet_id, $task_id, $target.$file_name);
				Routing::redirect('?q=tasksheet/showall');
			}
		}
		
	}
	
	public function newtasksheetstep1(){
		$sheet = $this->loader->model('tasksheet');
		if(!empty($_POST['step1check'])){
			$sortsheet=$sheet->NewTaskSheetStep1Check($_POST);
		}
		if(!empty($_POST['step1to2'])){
			$sortsheet=$sheet->NewTaskSheetStep1Check($_POST);
			$sortsheet['step1end']=1;
		}
		$this->smarty->assign('sheet', $sortsheet);
		$this->smarty->assign('title', 'Kémia éve - Új Feladatlap - 1. lépés');
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function newtasksheetstep2(){
		$sheet = $this->loader->model('tasksheet');
		$target= "uploads/temp/";
		$filenames=array();
		$i=1;
		if(!empty($_FILES)){
			foreach($_FILES as $file){
				$file_name = Tools::clear_url(utf8_decode($file['name']));
				$tmp_dir = $file['tmp_name'];
				if((!preg_match('/(jpg|jpeg|bmp|png|gif)$/i', $file_name))){
					$error='Rossz fileformátum!';
					$filenames[$i]=$target.$file_name;
					$i++;
				}
				else{
					if($tmp_dir!=''){
						$filenames[$i]=$target.$file_name;
						move_uploaded_file($tmp_dir, $target . $file_name);
					}
					else{
						$filenames[$i]='';
					}
					$i++;
				}
			}
			
		}
		
		if(isset($_POST)){
		for($v=0; $v<count($_POST); $v++){
			if($_POST[$v.'-video_link']!=""){
				$videos[$v] = Tools::get_video($_POST[$v.'-video_link']);
			}
		}
		}
		$sortsheet=$sheet->NewTaskSheetStep1Check($_POST);
		$sortsheet['step1end']=1;
		if(!empty($_POST['step2check'])){
			$sortsheet=$sheet->NewTaskSheetStep2Check($_POST,$sortsheet, $_FILES);
		}
		if(!empty($_POST['step2to3'])){
			$sortsheet=$sheet->NewTaskSheetStep2Check($_POST,$sortsheet,  $_FILES);
			$sortsheet['step2end']=1;
		}
		$this->smarty->assign('session_is_teacher', $_SESSION['is_teacher']);
		$this->smarty->assign('sheet', $sortsheet);
		$this->smarty->assign('title', 'Kémia éve - Új Feladatlap - 2. lépés');
		$this->smarty->assign('filenames', $filenames);
		$this->smarty->assign('videos', $videos);
	}
	
	public function newtasksheetstep3(){
		$sheet = $this->loader->model('tasksheet');
		$sortsheet=$sheet->NewTaskSheetStep1Check($_POST);
		$sortsheet['step1end']=1;
		$sortsheet=$sheet->NewTaskSheetStep2Check($_POST,$sortsheet,  $_FILES);
		$sortsheet['step2end']=1;
		if(!empty($_POST['step3check'])){
			$sortsheet=$sheet->NewTaskSheetStep3Check($_POST,$sortsheet);
		}
		if(!empty($_POST['step3to4'])){
			$sortsheet=$sheet->NewTaskSheetStep3Check($_POST,$sortsheet);
			$sortsheet['step3end']=1;
		}
		$this->smarty->assign('sheet', $sortsheet);
		$this->smarty->assign('title', 'Kémia éve - Új Feladatlap - 3. lépés');
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function newtasksheetstep4(){
		$sheet = $this->loader->model('tasksheet');
		$sortsheet=$sheet->NewTaskSheetStep1Check($_POST);
		$sortsheet['step1end']=1;
		$sortsheet=$sheet->NewTaskSheetStep2Check($_POST,$sortsheet,  $_FILES);
		$sortsheet['step2end']=1;
		$sortsheet=$sheet->NewTaskSheetStep3Check($_POST,$sortsheet);
		$sortsheet['step3end']=1;
		if(!empty($_POST['step4check'])){
			$id=$sheet->NewTaskSheetStep4Check($sortsheet);
			$tasks = $sheet -> GetTaskNumberCountBySheetId($id);
			if(!file_exists('uploads/tasks/'.$id)){ 
				mkdir('uploads/tasks/'.$id, 0777);
				mkdir('uploads/images/'.$id, 0777);
			}
			foreach ($tasks as $task) {
				if(!file_exists('uploads/tasks/'.$id.'/'.$task['number'])){
					mkdir('uploads/tasks/'.$id.'/'.$task['number'], 0777);
					mkdir('uploads/images/'.$id.'/'.$task['number'], 0777);
				}
			}
			
			foreach($sortsheet["tasks"] as $key => $task){
				if ($task['picture'] != NULL){
					$sheet ->ReallocateImage('uploads/temp/'.$sortsheet["tasks"][$key]['temp_picture'],'uploads/images/'.$id.'/'.$key.'/');
					$sheet->InsertTaskAttachment($id, $key, 'uploads/images/'.$id.'/'.$key.'/'.$sortsheet["tasks"][$key]['temp_picture'], $sortsheet["tasks"][$key]['video_link']);
				}
				else
					$sheet->InsertTaskAttachment($id, $key, '', $sortsheet["tasks"][$key]['video_link']);
			}
			Routing::redirect('?q=tasksheet/show/'.$id);
		}
		$this->smarty->assign('sheet', $sortsheet);
		$this->smarty->assign('title', 'Kémia éve - Új Feladatlap - 4. lépés');
		$this->smarty->assign('session_is_teacher', $_SESSION['is_teacher']);
	}
	
	public function delete($id){	
		$sheet = $this->loader->model('tasksheet');
		$sheet -> SelectTaskSheetFromSql($id);
		$is_ready = $sheet -> GetIs_Ready();
		if($is_ready == 0){
			$tasks = $sheet -> DeleteTaskSheetFromSQL($id); //tasks[0] = 1 ;tasks[1] = 5 ; ....
			for($i=0; $i<count($tasks); $i++){
				$answers[$i] = $sheet -> DeleteTaskFromSQL($tasks[$i]['id']);
			}
			for($i=0; $i<count($answers); $i++){
				for($j=0; $j<count($answers[$i]); $j++){
					$sheet -> DeleteAnswerFromSQL($answers[$i][$j]['id']);
				}
			}
			$t=scandir('uploads/images/'.$id);
			for($i=1;$i<(count($t)-1);$i++){
				$temp=scandir('uploads/images/'.$id.'/'.$i);
				if($temp[2]!=''){
					unlink('uploads/images/'.$id.'/'.$i.'/'.$temp[2]);
				}
			}
			for($i=1;$i<(count($t)-1);$i++){
				rmdir('uploads/images/'.$id.'/'.$i);
			}
			rmdir('uploads/images/'.$id);
			
			$t=scandir('uploads/tasks/'.$id);
			for($i=1;$i<(count($t)-1);$i++){
				rmdir('uploads/tasks/'.$id.'/'.$i);
			}
			rmdir('uploads/tasks/'.$id);
			Routing::redirect('?q=tasksheet/showall');
		}
		$this->smarty->assign('session_is_teacher', $_SESSION['is_teacher']);
		$this->smarty->assign('is_ready', $is_ready);
		$this->smarty->assign('title', 'Kémia éve - Feladatlap Törlése');
	}
	
	function RateUpload($tasksheet_id, $user_id){
		$sheet = $this->loader->model('user');
		$temp=$sheet->SelectUserFromSql($user_id);
		$username=$temp[0]['username'];
		$sheet = $this->loader->model('tasksheet');
		$file=$sheet->GetUserAttachment2($tasksheet_id, $user_id);
		$max= $sheet->GetMaxScore($tasksheet_id);
		$this->smarty->assign('tasksheet_id', $tasksheet_id);
		$this->smarty->assign('u_id', $user_id);
		$this->smarty->assign('u_name', $username);
		$this->smarty->assign('file', $file);
		$this->smarty->assign('max', $max);
		$this->smarty->assign('title', 'Kémia éve - Feladatlap értékelése');
		if(!empty($_POST['rate'])){
			if($_POST['comment']!='') $comment = $sheet->MakeComment($username, $_POST['comment']);
			$sheet->InsertUploadRate($tasksheet_id, $user_id, $_POST['score'], $_SESSION['username'], $comment);
			Routing::redirect('?q=tasksheet/show/'.$tasksheet_id);
		}
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function alreadysubmited(){	}
	
	function editupload($tasksheet_id, $user_id){
		$sheet = $this->loader->model('user');
		$temp=$sheet->SelectUserFromSql($user_id);
		$username=$temp[0]['username'];
		$sheet = $this->loader->model('tasksheet');
		$file=$sheet->GetUserAttachment2($tasksheet_id, $user_id);
		$temp=$sheet->GetUploadRate($tasksheet_id, $user_id);
		$max= $sheet->GetMaxScore($tasksheet_id);
		$this->smarty->assign('data', $temp);
		$this->smarty->assign('tasksheet_id', $tasksheet_id);
		$this->smarty->assign('u_id', $user_id);
		$this->smarty->assign('u_name', $username);
		$this->smarty->assign('file', $file);
		$this->smarty->assign('max', $max);
		$this->smarty->assign('title', 'Kémia éve - Értékelés javítása');
		

		if(!empty($_POST['rate'])){
			if($_POST['comment']!='') $comment = $sheet->MakeComment($_SESSION['username'], $_POST['comment'], $temp[0]['comment']);
			$sheet->UpdateUploadRate($tasksheet_id, $user_id, $_POST['score'], $_SESSION['username'], $comment);
			Routing::redirect('?q=tasksheet/show/'.$tasksheet_id);
		}
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function edit($id){
		//var_dump($_POST);
		$tasksheet = $this->loader->model('tasksheet');
		$tasksheet->SelectTaskSheetFromSql($id);
			
		if(!empty($_POST)){
			$type = explode('-' , $_POST['type']);
			if($type[2] == 'changeinfo')
				$tasksheet->Edit_ChangeInfo($type[0],$type[1],$_POST);
			if($type[2] == 'changedescription')
				$tasksheet->Edit_ChangeDescription($type[0],$type[1],$_POST);
			if($type[2] == 'modifyanswer')
				$tasksheet->Edit_ModifyAnswer($type[0],$type[1],$_POST);
			/*if($type == 'changeimage'){}
				$tasksheet->Edit_ModifyImage($type[0],$type[1],$_POST);
			if($type == 'newimage'){}
				$tasksheet->Edit_NewImage($type[0],$type[1],$_POST);
			if($type == 'changevideo'){}
				$tasksheet->Edit_ModifyVideo($type[0],$type[1],$_POST);
			if($type == 'newvideo'){}
				$tasksheet->Edit_NewVideo($type[0],$type[1],$_POST);*/
			if($type[2] == 'newanswer')
				$tasksheet->Edit_NewAnswer($type[0],$type[1],$_POST);
			if($type[2] == 'newtask')
				$tasksheet->Edit_NewTask($type[0],$type[1],$_POST);
			if($type[2] == 'deletetask')
				$tasksheet->Edit_DeleteTask($type[0],$type[1],$_POST);
			if($type[2] == 'deleteanswer')
				$tasksheet->Edit_DeleteAnswer($type[0],$type[1],$_POST);
			Routing::redirect('?q=tasksheet/edit/'.$id);
		}
		
		$task_attachment =  $tasksheet-> SelectTaskSheetAttachmentFromSql($id);
		$user_attachment = $tasksheet -> GetUserAttachment($id, '1');
		$task = $this->loader->model('task');
		$tasks = $task->SelectAllTaskFromSql($id);
		$taskcount = count($tasks);
		$answer = $this->loader->model('answer');
		$answers = $answer->SelectAllAnswerFromSql();
		$answercount = count($answers);
		$this->smarty->assign('tasksheet', $tasksheet);
		$this->smarty->assign('task_attachment', $task_attachment);
		$this->smarty->assign('user_attachment', $user_attachment);		
		$this->smarty->assign('tasks', $tasks);
		$this->smarty->assign('taskcount', $taskcount);
		$this->smarty->assign('is_upload', $tasks[0]['is_upload']);		
		$this->smarty->assign('answers', $answers);
		$this->smarty->assign('answercount', $answercount);
		$this->smarty->assign('id', $id);

		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
		$this->smarty->assign('title', 'Kémia éve - Feladatlap Módosítása');
	}
	
}
?>